import React, { useState, useEffect } from 'react'
import { Input, message, Modal, Select, Form } from 'antd';
import { AddPhysical, Edit_controllers, Getxianshireader, SetController } from './service'
const AddModelPhysical = ({ isModalOpen, setIsModalOpen, getDataSource, EditMsg }) => {



    const [value, setValue] = useState([]);

    const [list, setList] = useState([])

    const [form] = Form.useForm();

    useEffect(() => {

        let arr = []
        for (var i = 0; i < 50; i++) {
            // arr.push( { label: `192.168.0.25${i}`, value: `192.168.0.25${i}` })
        }

        Getxianshireader().then(res => {
            console.log(res)
            res.readerliebiao.forEach((item) => {
                if (item.ip_address) {

                    arr.push({ label: item.controller_id, value: item.controller_id })
                }

            })
            console.log("结果 ->", arr)
            setList(arr);
        })

        // console.log(EditMsg)

        form.setFieldsValue({
            physical_zone: EditMsg.physical_zone,

        })
    }, [])

    const handleOk = () => {

        form.validateFields().then(async values => {

            let res = await SetController({
                controller_id: values.controller_id,
                controller_name: values.controller_name,
                physical_zone: EditMsg.physical_zone,
                note: values.note
            })
            console.log(res)
            if (res.result != 0) return message.error(res.msg)
            message.success("添加成功")
            getDataSource()
            setIsModalOpen(false)
            // Edit_controllers(EditMsg.id, values).then(res => {
            //     console.log(res)
            //     if (res.code == 0) {


            //     } else {
            //         message.error(res.msg)

            //     }
            // })
        })


    };



    return (
        <Modal
            title={"编辑控制器"}
            closable={{ 'aria-label': 'Custom Close Button' }}
            visible={isModalOpen}
            onOk={handleOk}
            onCancel={() => setIsModalOpen(false)}
        >
            <div style={{ padding: 30 }}>


                <Form
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 16 }}
                    form={form}
                >

                    <Form.Item label="物理区名称"
                        name="physical_zone"
                    >
                        <Input
                            disabled={true}
                            placeholder={"请输入控制器名称"}
                            style={{ width: '100%' }}
                            value={value}
                            onChange={(e) => setValue(e.target.value)}
                        />
                    </Form.Item>

                    <Form.Item label="控制器名称"
                        name="controller_name"
                        rules={[{ required: true, message: '请输入控制器名称' }]}
                    >
                        <Input

                            placeholder={"请输入控制器名称"}
                            style={{ width: '100%' }}

                        />
                    </Form.Item>


                    <Form.Item label="控制器IP" name="controller_id"
                        rules={[{ required: true, message: '请选择控制器!' }]}
                    >
                        <Select
                            options={list}
                            placeholder={"请选择控制器"}
                            style={{ width: '100%' }}
                            value={value}

                            onChange={(value) => setValue(value)}
                        />
                    </Form.Item>

                    <Form.Item label="备注" name="note" >
                        <Input
                            placeholder={"请输入备注"}

                        />
                    </Form.Item>
                </Form>





            </div>
        </Modal>
    )
}

export default AddModelPhysical